package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import static com.zzyl.common.constant.CacheConstants.CACHE_NURSING_PLAN_ALL;
import static com.zzyl.common.constant.CacheConstants.CACHE_NURSING_PROJECT_ALL;

/**
 * 护理项目Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;


    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        int insert = nursingProjectMapper.insert(nursingProject);
        extracted();
        return insert;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        int updateById = nursingProjectMapper.updateById(nursingProject);
        extracted();
        return updateById;
    }

    private void extracted() {
        redisTemplate.delete(CACHE_NURSING_PROJECT_ALL);
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        extracted();
        return nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        extracted();
        return nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {

        //1.查询redis缓存中的所有护理等级
        String result = redisTemplate.opsForValue().get(CACHE_NURSING_PROJECT_ALL);
        //2.如果缓存中有数据，将JSON字符串转换成集合直接返回
        if (StringUtils.isNotEmpty(result)) {
            return JSON.parseArray(result, NursingProjectVo.class);
        }
        //3.如果缓存中没有数据，查询数据库
        List<NursingProjectVo> allProjects = nursingProjectMapper.getAllProjects();

        //4.将查询到的数据写入缓存
        redisTemplate.opsForValue().set(CACHE_NURSING_PROJECT_ALL, JSON.toJSONString(allProjects));
        //5.返回查询到的数据

        return allProjects;
    }

    @Override
    public NursingProject selectProject(NursingProject nursingProject) {
        
        return null;
    }
}
